﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Biblioteca.DataBase;
using Pms.Utils;
using System.Data;
using SEG.Utils;
using Biblioteca.Utils;
using System.Data.Common;
using Biblioteca.Objetos;

namespace SEG
{
    public partial class Interfaces : System.Web.UI.Page
    {
        Usuario usuAut;


        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session.Count == 0)
                Response.Redirect("~/Login.aspx");
            else
            {
                usuAut = (Usuario)Session["usuario"];
                char perm = BiblioPMS.VerifiquePermissao("INTERFACES.ASPX", BiblioPMS.aplicacaoSEG, usuAut.Login);
                if (!perm.Equals('M'))
                    Response.Redirect("Inicio.aspx");
                
                lblErro.Visible = false;

                if (!Page.IsPostBack)
                {
                    Session.Add("pagAtual", 1);
                    ddlAplicacoes.DataBind();
                }

               Pesquisar();
            }

        }

        protected void btnNovo_Click(object sender, EventArgs e)
        {
            Response.Redirect("InterfacesManut.aspx?OP=I");
        }

        protected void btnPesquisar_Click(object sender, EventArgs e)
        {
            Pesquisar();
        }

        private void Pesquisar()
        {
            string sql = @"select cod_aplic, cod_int, dsc_int, dsc_aplic from seg_interfaces natural join seg_aplicacoes where cod_aplic =:cod_aplic order by dsc_int";

            QueryPaginada qry = new QueryPaginada(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSEG, usuAut.Login), sql, BiblioSEG.tamPag);
            qry.AdicionarParametro("cod_aplic", DbType.String, ddlAplicacoes.SelectedValue);


            try
            {
                gvInterfaces.DataSource = qry.Executar((int)Session["pagAtual"]);
                gvInterfaces.DataBind();
                CarregueBotoes(qry.QtdLinhas());
            }
            catch
            {

                Todos();

            }
            finally
            {
                qry.FecharConexao();
            }
        }

        private void Todos()
        {
            string sql = @" select cod_aplic, cod_int, dsc_int, dsc_aplic from seg_interfaces natural join seg_aplicacoes ";

            QueryPaginada qry = new QueryPaginada(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSEG, usuAut.Login), sql, BiblioSEG.tamPag);
            qry.AdicionarParametro(":cod_aplic", DbType.String, ddlAplicacoes.SelectedValue);


            try
            {
                gvInterfaces.DataSource = qry.Executar((int)Session["pagAtual"]);
                gvInterfaces.DataBind();
                CarregueBotoes(qry.QtdLinhas());
            }
            catch(Exception ex)
            {
                lblErro.Visible = true;
                lblErro.Text = "*Erro: " + ex.Message;

            }
            finally
            {
                qry.FecharConexao();
            }
            
        }

        protected void gvInterfaces_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string cod_aplic = ((DbDataRecord)e.Row.DataItem).GetDecimal(1).ToString();
                string cod_int = ((DbDataRecord)e.Row.DataItem).GetDecimal(2).ToString();
                
                    ((HyperLink)e.Row.Cells[2].Controls[1]).NavigateUrl = "InterfacesManut.aspx?OP=D&codInt=" + Server.UrlEncode(Criptografia.Criptografar(cod_int)) + "&codAplic=" + Server.UrlEncode(Criptografia.Criptografar(cod_aplic));

            }    


        }


        private void CarregueBotoes(int qtdLinhas)
        {
            Biblio.CarregueTela(lblPagina, lblTotalPag, Session, BiblioSEG.tamPag, qtdLinhas);
            Biblio.ValideBotoes(btnPrimeiro, btnAnterior, btnProximo, btnUltimo, (int)Session["pagAtual"], int.Parse(lblTotalPag.Text));
        }

        protected void btnPrimeiro_Click(object sender, EventArgs e)
        {
            Session.Add("pagAtual", 1);
            Pesquisar();
        }

        protected void btnAnterior_Click(object sender, EventArgs e)
        {
            Session["pagAtual"] = (int)Session["pagAtual"] - 1;
            Pesquisar();
        }

        protected void btnProximo_Click(object sender, EventArgs e)
        {
            Session["pagAtual"] = (int)Session["pagAtual"] + 1;
            Pesquisar();
        }

        protected void btnUltimo_Click(object sender, EventArgs e)
        {
            Session["pagAtual"] = Session["totalPag"];
            Pesquisar();
        }

        protected void ddlAplicacoes_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session.Add("pagAtual", 1);
            Pesquisar();
        }
    }
}
